package com.grj.parking.controller;

import com.grj.parking.a.ParkingService;
import com.grj.parking.aa.ParkingStrategyFactory;
import com.grj.parking.config.Result;
import com.grj.parking.domain.ParkingRecord;
import com.grj.parking.domain.Vehicle;
import com.grj.parking.mapper.ParkingRecordMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


@RestController
@RequestMapping("/api/v1/parking")
@Api(tags = "停车管理接口", description = "提供停车的功能")
@RequiredArgsConstructor
public class ParkingController {
    private final ParkingService parkingService;
    private final ParkingStrategyFactory parkingStrategyFactory;
    private final ParkingRecordMapper parkingRecordMapper;

    @ApiOperation(value = "车辆进场", notes = "车辆出场")
    @PostMapping("/entry")
    public Result<Boolean> entry(String carNum, Long lotId) throws Exception {
        // 1.确认停车场
        parkingService.checkParkingLot(lotId);
        // 2.车牌验证
        Vehicle vehicle = parkingService.checkPlate(carNum);
////        // 3.检查是否存在未结算订单
//        parkingService.checkOrder(carNum);
        // 4.车位分配
        parkingService.allocateParkingSpace(lotId, vehicle);
        return Result.success(true);
    }


    @ApiOperation(value = "车辆出场", notes = "车辆出场")
    @PostMapping("/exit")
    public Result<Boolean> exit(String carNum, Long lotId) throws Exception {
        // 1.查询停车记录
        ParkingRecord parkingRecord = parkingService.checkEntryOrder(carNum);
        // 2.计算停车费
        ParkingRecord parkingRecord2 = parkingService.calculateParkingFees(parkingRecord);
        // 3.下发开闸指令
        parkingService.issueOpenGateCommand(parkingRecord2);

        // 6.释放停车位，修改停车场空闲停车位

        return Result.success(true);
    }

    @ApiOperation(value = "策略", notes = "策略")
    @PostMapping("/strategy")
    public Result<String> strategy(Long id) {
        ParkingRecord parkingRecord = parkingRecordMapper.selectById(id);
        ParkingRecord parkingResult = parkingStrategyFactory.getStrategy(parkingRecord.getVehicleType(), parkingRecord);
        return Result.success("");
    }

    @ApiOperation(value = "es", notes = "es")
    @PostMapping("/es")
    public Result<String> es() throws IOException {
//        parkingLotSyncService.fullSync();
        return Result.success("");
    }

    @ApiOperation(value = "es2", notes = "es2")
    @PostMapping("/es2")
    public Result<String> es2() throws IOException {
//        parkingLotSyncService.syncById(1L);
        return Result.success("");
    }


}
